<?php
/**
 * @author  xzh
 * @todo管理
 * 2020.10.1
 */
	namespace classes;
	class Invite extends \classes\Base
	{

        //0待审核1已通过2已拒绝,默认空表示全部
        public $status = array(
            array('id'=>'','name'=>'全部'),
            array('id'=>2,'name'=>'待处理'),
            array('id'=>3,'name'=>'已通过'),
            array('id'=>4,'name'=>'已拒绝')
        );
        //企业邀请
        public function index(){
            $pm = $this->GetRouter("compid");
            $compid = $pm->GetValue("compid");
            $array['act'] = $this->token['act'];
            $array['token'] = $this->token['token'];
            $array['compid'] = $this->compid;
            $array['status'] = '0';
            $totals = $this->Yunkong->CompanyGetCheckList($array)['totals'];
            $data = $this->Yunkong->AccountGetInviteList($array);

            $num = count($data['data']);
            //输出
            $this->Assign('num', $num);
            $this->Assign('totals', $totals);
            $this->Display();
        }
        //接口
        public function AccountInvite(){
            $pm = $this->GetRouter("compid,page");
            $compid = $pm->GetValue("compid");
            $page = $pm->HasValue("page") ? $pm->GetValue("page") : '';

            $pp = $this->GetPoster();
            $keyword = $pp->HasValue("name") ? $pp->GetValue("name") : '';
            $status = $pp->HasValue("status") ? $pp->GetValue("status") : '';

            $array['act'] = $this->token['act'];
            $array['token'] = $this->token['token'];
            $array['compid'] = $compid;
            $array['page'] = $page;
            $array['keyword'] = $keyword;
            $array['status'] = $status;
            $data = $this->Yunkong->AccountGetInviteList($array);

            $i = 1;
            if(is_array($data)){
                foreach ($data['data'] as $key=>$val){
                    $data['data'][$key]['name'] = $val['name']==''?'暂无':$val['name'];
                    $data['data'][$key]['rows'] = $i;
                    $i++;
                }
                echo json_encode(array('status'=>0,'data'=>$data));exit;
            }
            echo json_encode(array('status'=>0,'data'=>array()));exit;
        }
        public function Pass(){
            $pm = $this->GetRouter("id");
            $array['act'] = $this->token['act'];
            $array['token'] = $this->token['token'];
            $array['id'] = $pm->GetValue("id");
            $res = $this->Yunkong->AccountPassInvite($array);
            if($res['status']==0){
                $this->Success("成功加入企业",'index/index','5','parent');
            }
            $this->Error("邀请失败，请重新再试");
        }
        public function Rejected(){
            $pm = $this->GetRouter("id");
            $array['act'] = $this->token['act'];
            $array['token'] = $this->token['token'];
            $array['id'] = $pm->GetValue("id");
            $res = $this->Yunkong->AccountRejectedInvite($array);
            if($res['status']==0){
                $this->Success("拒绝邀请",'index/index','5','parent');
            }
            $this->Error("拒绝失败，请重新再试");
        }
        

        //生成邀请路由
        public function Invite(){
            $pm = $this->GetRouter("compid,bmbh");
            $compid = $pm->GetValue("compid");
            $bmbh = $pm->HasValue("bmbh") ? $pm->GetValue("bmbh") : "";
            $CompanyName = $this->Jurisdiction($compid,2);

            $aes = new \core\AES();

            $arr = array();
            $arr['time'] = strtotime(date('Y-m-d H:i:s',strtotime('+1day')));
            $arr['inviter'] = $this->token['act'];

            $Invite = implode(',',$arr);
            $base = $aes->Encrypt($Invite,true);


            $http = 'http://'.$_SERVER['HTTP_HOST'];
            $url = $http.'/register/Inviter/'.$base;
            //print_r($url);
            echo json_encode(array("status"=>0,"url"=>$url));exit;
        }
        //添加成员
        public function InviteUrl(){
            $this->Display('Invite/url');
        }
        //验证邀请
        public function Invitation(){
            $pm = $this->GetRouter("compid,inviter");
            $base = base64_decode($pm->GetValue("inviter"));
            $arr = explode(',',$base);

            if($arr[0]!='YUNkong'){
                $this->Error("邀请码出错，请重新获取",'Register/index/'.$pm->GetValue("inviter"));
            }
            $time = $arr[1];
            $inviter = $arr[2];
            if(empty($inviter)){
                $this->Error("邀请码出错，请重新获取",'Register/index/'.$pm->GetValue("inviter"));
            }
            if($time){
                $date = time();
                if($time < $date){
                    $this->Error("邀请时间过期",'Register/index/'.$pm->GetValue("inviter"));
                }
            }else{
                $this->Error("邀请时间过期",'Register/index/'.$pm->GetValue("inviter"));
            }
            if($this->top['data']){
                foreach ($this->top['data'] as $key=>$val){
                    if($compid == $val['id']){
                        $this->Error("已加入该企业，不可重复加入",'index/index');
                        break;
                    }
                }
            }
            if($this->token['act']==$inviter){
                $this->Error("请勿邀请本入",'index/index');
            }
            //账户是否登录
            if(empty($this->token['act'])){
                $this->Error("请先注册",'Register/index/'.$pm->GetValue("inviter"));
            }
            $array['act'] = $this->token['act'];
            $array['token'] = $this->token['token'];
            $array['inviter'] = $inviter;
            $res = $this->Yunkong->AccountInvite($array);
            if($res['status']==0){
                $this->Success('请在企业邀请菜单中处理邀请信息','index/index');
            }
            $this->Error($res['msg'],'index/index');
        }








    }
?>